

app.controller("AboutIndexController", ['$location', '$scope', "$rootScope", function($location, $scope, $rootScope){
	$rootScope.pageTitle = "关于云车生活";
	$rootScope.pageClass = "page-about";
	$rootScope.showBackButton = true;
	$rootScope.canRemoveAction = false;
	$scope.aboutContent=function(){
		$location.path('/about/content').search({backUrl: '/about/'});
	}
	$scope.aboutPrivacy=function(){
		$location.path('/about/privacy').search({backUrl: '/about/'});
	}
}]);

app.controller("AboutContentController", ['$location', '$scope', "$rootScope", function($location, $scope, $rootScope){
	$rootScope.pageTitle = "关于我们";
	$rootScope.pageClass = "page-about";
	$rootScope.showBackButton = true;
	$rootScope.canRemoveAction = false;

}]);

app.controller("AboutPrivacyController", ['$location', '$scope', "$rootScope", function($location, $scope, $rootScope){
	$rootScope.pageTitle = "用户协议和隐私声明";
	$rootScope.pageClass = "page-about";
	$rootScope.showBackButton = true;
	$rootScope.canRemoveAction = false;

}]);

app.controller("EnrollStatusController", ['$location','$scope', "$modal", "$rootScope", "$routeParams","snsService",function($location, $scope, $modal, $rootScope, $routeParams,snsService){
	$rootScope.pageTitle = "我的报名";
	$rootScope.pageClass = "page-enroll-status";
	$rootScope.canCreateTopic = false;
	$rootScope.canRemoveAction = false;
	$rootScope.hasFooter = false;
	$rootScope.enrollInfo = {};
	$rootScope.enrollInfo.gender = 0;
	$rootScope.isPending = true;
	snsService.getEnrollmentProfiles().then(function(data){
		$rootScope.isPending = false;
		$scope.enrollStatus = data.data;
	});
    $scope.continuePay=function(orderId){
        $rootScope.learnDrivingPayOrderId=orderId;
        $location.path("/choosePayType/"+payScene.LDE).search({backUrl:"/enrollStatus"})
    }
}]);

app.controller("EnrollController", ['$location','$scope', "$modal", "$rootScope", "$routeParams","snsService",function($location, $scope, $modal, $rootScope, $routeParams,snsService){
	$rootScope.pageTitle = "报名学车";
	$rootScope.pageClass = "page-enroll";
	$rootScope.canCreateTopic = false;
	$rootScope.canRemoveAction = false;
	$rootScope.hasFooter = false;
	$rootScope.enrollInfo = {};
	$rootScope.enrollInfo.gender = 0;
    $rootScope.enrollInfo.payMethod=payMethod.prepaidOnline;//默认设置成在线预付
	$rootScope.enrollInfo.payMethodDes="在线预付";
	$rootScope.selectedRegion = $rootScope.selectedRegion?$rootScope.selectedRegion:{};


	var getProvince = function(){
		$rootScope.isPending = true;
		snsService.getProvince().then(function(data){
			console.log("getProvince", data)
			$rootScope.provinces = data;
			var defaultRegion = findCity(0);
			if(defaultRegion){
				$rootScope.selectedRegion.provinceName = defaultRegion.province.name;
				$rootScope.selectedRegion.cityName = defaultRegion.city.name;
				$rootScope.selectedRegion.cityId = defaultRegion.city.cityId;
				getSchool(defaultRegion.city.cityId);
			}else{
				$rootScope.isPending = false;

			}
			
		});	
	};
	var getSchool = function(cityId){
		$rootScope.isPending = true;
		snsService.getDrivingSchool({
			cityId: cityId
		}).then(function(data){
			if(data.status==0 && data.data.length){
				$rootScope.schools = data.data;	
				$rootScope.selectedRegion.drivingSchoolId = data.data[0].drivingSchoolId;
				$rootScope.selectedRegion.schoolName = data.data[0].schoolName;
				//console.log(data.data)
			}
			else{

			}

		});
	}
	var findCity = function(cityId){
		for(var i=0;i<$rootScope.provinces.length;i++){
			for(var j=0;j<$rootScope.provinces[i].cities.length;j++){
				if(cityId == $rootScope.provinces[i].cities[j].cityId){

					return {
						province: {name:$rootScope.provinces[i].name, provinceId:$rootScope.provinces[i].provinceId},
						city: {name:$rootScope.provinces[i].cities[j].name, cityId:cityId}
					};
				}

			}
		}
		return false;

	}


	

	$scope.selectRegion = function(){
		//console.log("select region")
		schoolWatcher();
		carWatcher();
		classWatcher();
        $rootScope.canExit = false;
		$location.path("/region");
	};
	$scope.selectSchool = function(){
        $rootScope.canExit = false;
		$location.path("/drivingSchool");
	};
	$scope.updateCarType = function(id){
		$rootScope.selectedRegion.selectedCarType = $scope.carTypeOptions[id];
	};	
	$scope.updateClassType = function(id){
		$rootScope.selectedRegion.selectedClassType = $scope.classTypeOptions[id];
	};	
	$scope.updateGenderType = function(gender){
		$rootScope.enrollInfo.gender = gender;
	};

    $scope.updatePayMethod = function(payMethodValue){
        $rootScope.enrollInfo.payMethod = payMethodValue;
		if(payMethodValue==payMethod.payAllOnline)
			$rootScope.enrollInfo.payMethodDes="在线全付";
		else  if(payMethodValue==payMethod.prepaidOnline)
			$rootScope.enrollInfo.payMethodDes="在线预付";
		else  if(payMethodValue==payMethod.payOffline)
			$rootScope.enrollInfo.payMethodDes="线下支付";;
    };
	$scope.enroll = function(){	
		var errMessage;
		if(!getIdCardInfo($rootScope.enrollInfo.idNum || "").isTrue){
			errMessage = "您填写的身份证号码无效";
		}
		if(!$rootScope.enrollInfo.contactNum || !$rootScope.enrollInfo.name || !$rootScope.enrollInfo.idNum || !$rootScope.selectedRegion.drivingSchoolId){
			errMessage = "您的个人信息不完整";
		}
		if(errMessage){
			var modalInstance = $modal.open({
				templateUrl: 'views/tmpl_modal_alert.html',
				controller: ModalAlertCtrl,
				size: 'sm',
				resolve: {
					alertObj: function () {							
						return {message:errMessage};
					}
				}
			});
			return;
		};
		var classTypeId = $rootScope.selectedRegion.selectedClassType?$rootScope.selectedRegion.selectedClassType.learnDrivingClassTypeId:''
        $rootScope.enrollInfo.payMethod= $rootScope.selectedRegion.selectedClassType?$rootScope.enrollInfo.payMethod:payMethod.undefined;//未选择车型，
		var params = {
			"contactNum": $rootScope.enrollInfo.contactNum,
			"name": $rootScope.enrollInfo.name,
			"idNum": $rootScope.enrollInfo.idNum,
			"drivingSchoolId": $rootScope.selectedRegion.drivingSchoolId,
			"gender": $rootScope.enrollInfo.gender,
			"learnDrivingClassTypeId": classTypeId,
			"payMethod":$rootScope.enrollInfo.payMethod,
			"payType": 0
		};
		var modalInstance = $modal.open({
			templateUrl: 'views/tmpl_modal_confirm.html',
			controller: ModalConfirmCtrl,
			size: 'sm',
			resolve: {
				enrollInfo: function () {							
					return params;
				}
			}
		});
		
	
	};
	
	// watch the selected school, call relevant API
	var schoolWatcher = $rootScope.$watch("selectedRegion.drivingSchoolId", function( newVal ) { 
		if(!newVal) return;
		if($rootScope.selectedRegion.schoolName){
			$rootScope.isPending = true;
			snsService.getCarTypes({
				drivingSchoolId: $rootScope.selectedRegion.drivingSchoolId
			}).then(function(data){
				if(data.data.length){
					$scope.carTypeOptions = data.data;			
					$scope.selectedRegion.selectedCarType = data.data[0];	
				}
				else{
					$rootScope.isPending = false;
				}	
			});
		}		
	});
	// watch the selected car type, call class API
	var carWatcher = $rootScope.$watch("selectedRegion.selectedCarType.learnDrivingCarTypeId", function( newVal ) { 
		if(!newVal) return;
		if($rootScope.selectedRegion.selectedCarType){
			$rootScope.isPending = true;
			snsService.getClassTypes({
				learnDrivingCarTypeId: $rootScope.selectedRegion.selectedCarType.learnDrivingCarTypeId
			}).then(function(data){
				if(data.data.length){
					$scope.classTypeOptions = data.data;	
					$scope.selectedRegion.selectedClassType = data.data[0];			
				}			
			});
		}		
	});
	// watch the selected class type, call price API
	var classWatcher = $rootScope.$watch("selectedRegion.selectedClassType.learnDrivingClassTypeId", function( newVal ) { 
		if(!newVal) return;
		//console.log("selectedRegion.selectedClassType", $rootScope.selectedRegion.selectedClassType)
		if($rootScope.selectedRegion.selectedClassType){
			snsService.getClassPrice({
				learnDrivingClassTypeId: $rootScope.selectedRegion.selectedClassType.learnDrivingClassTypeId
			}).then(function(data){
				//console.log(data)
				$rootScope.isPending = false;
				$rootScope.selectedRegion.selectedClassPrice = data.data.price;
				$rootScope.selectedRegion.selectedPrepayment=data.data.prepayment;
			});
		}		
	});



	if(!$rootScope.provinces){
		getProvince();
	}
	else{


	}

}]);
app.controller("RegionController", ['$location','$scope', "$rootScope", "$routeParams","snsService",function($location, $scope, $rootScope, $routeParams,snsService){
	$rootScope.pageTitle = "选择地区";
	$rootScope.canRemoveAction = false;
	$rootScope.pageClass = "page-region";
	$rootScope.cities = [];
	$scope.selectRegion = function(){
		console.log("select region");
	};
	if($rootScope.regionWatcher){
		$rootScope.regionWatcher();
	}

}]);


app.controller("DrivingSchoolController", ['$location','$scope', "$rootScope", "$routeParams","snsService",function($location, $scope, $rootScope, $routeParams,snsService){
	$rootScope.pageTitle = "选择驾校";
	$rootScope.canRemoveAction = false;
	$rootScope.pageClass = "page-school";
	$rootScope.schools = [];
	$scope.hasSchool = false;

	$rootScope.regionWatcher = $rootScope.$watch("selectedRegion", function( newVal ) { 
		//console.log("$rootScope.provinces[provinceIndex]", $rootScope.cities);
		if(!newVal) return;
		//console.log(cityScroll.wrapper);
		if($rootScope.selectedRegion.provinceName && $rootScope.selectedRegion.cityName){
			$rootScope.isPending = true;
			snsService.getDrivingSchool({
				cityId: $rootScope.selectedRegion.cityId
			}).then(function(data){
				if(data.status==0 && data.data.length){
					$rootScope.schools = data.data;		
				}
				else{

				}
				$rootScope.isPending = false;
			});
		}
	});
	
}]);
